load("//tools:defaults.bzl", "ng_module", "ng_package")
load("//tools/highlight-files:index.bzl", "highlight_files")
load("//tools/package-docs-content:index.bzl", "package_docs_content")
load(":config.bzl", "ALL_EXAMPLES")

package(default_visibility = ["//visibility:public"])

ng_module(
    name = "components-examples",
    srcs = glob(["*.ts"]) + [":example-module.ts"],
    deps = ALL_EXAMPLES,
)

filegroup(
    name = "example-source-files",
    srcs = ["%s:source-files" % pkg for pkg in ALL_EXAMPLES],
)

highlight_files(
    name = "examples-highlighted",
    srcs = [":example-source-files"],
)

package_docs_content(
    name = "docs-content",
    srcs = {
        # We want to package the guides in to the docs content. These will be displayed
        # in the documentation.
        "//guides": "guides",

        # Package the overviews for "@angular/material" and "@angular/cdk" into the docs content
        "//src/cdk:overviews": "overviews/cdk",
        "//src/material:overviews": "overviews/material",

        # Package the extracted token information into the docs content.
        "//src/material:tokens": "tokens/material",

        # Package the API docs for the Material and CDK package into the docs-content
        "//src:api-docs": "api-docs",

        # In order to be able to run examples in StackBlitz, we also want to package the
        # plain source files into the docs-content.
        ":example-source-files": "examples-source",

        # For the live-examples in our docs, we want to package the highlighted files
        # into the docs content. These will be used to show the source code for examples.
        # Note: `examples-highlighted` is a tree artifact that we want to store as is
        # in the docs-content. Hence there is no target section name.
        ":examples-highlighted": "",
    },
)

ng_package(
    name = "npm_package",
    srcs = ["package.json"],
    # this is a workaround to store a tree artifact in the ng_package.
    # ng_package does not properly handle tree artifacts currently so we escalate to nested_packages
    nested_packages = [":docs-content"],
    tags = ["docs-package"],
    deps = [":components-examples"] + ALL_EXAMPLES,
)

genrule(
    name = "example-module",
    srcs = [":example-source-files"],
    outs = [
        "example-module.ts",
        "_example_module.MF",
    ],
    cmd = """
      # Create source file manifest
      echo "$(execpaths //src/components-examples:example-source-files)" \
          > $(execpath _example_module.MF)

      # Run the bazel entry-point for generating the example module.
      ./$(execpath //tools/example-module:bazel-bin) \
          "$(execpath _example_module.MF)" \
          "$(execpath example-module.ts)" \
          "$$PWD/src/components-examples"
    """,
    output_to_bindir = True,
    tools = ["//tools/example-module:bazel-bin"],
)
